# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_pyname=PyMuPDF
_realname=pymupdf
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=1.21.1
pkgrel=1
pkgdesc="Python bindings for MuPDF's rendering library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/pymupdf/PyMuPDF'
license=('spdx:AGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gumbo-parser"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-jbig2dec"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-mupdf-libmupdf"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-distro"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-wheel"
             "${MINGW_PACKAGE_PREFIX}-swig")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-fonttools: for building font subsets using fontTools"
            "${MINGW_PACKAGE_PREFIX}-python-pillow: for image file saving using pillow")
source=("https://pypi.org/packages/source/${_pyname::1}/${_pyname}/${_pyname}-${pkgver}.tar.gz"
        0001-setup-add-mingw.patch)
sha256sums=('f815741a435c62a0036bbcbf5fa6c533567bd69c5338d413714fc57b22db93e0'
            'f698a9bdf24df0edabf20f03b2d20d9d16a13598860e8fe56d9de6174be1764d')

prepare() {
  cd "${srcdir}/${_pyname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-setup-add-mingw.patch"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}"
  cp -r "${_pyname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  # Use system provided mupdf libraries
  PYMUPDF_SETUP_MUPDF_BUILD="" \
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  msg "Python install for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"
}
